<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true">
      <el-row>
        <el-form-item label="处理单编码">
          <el-input
            v-model.trim="queryParams.wasteCode"
            clearable
            style="width: 180px"
            placeholder="请输入处理单编码"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="处理单位">
          <el-input
            v-model.trim="queryParams.handleDept"
            clearable
            style="width: 180px"
            placeholder="请选择处理单位"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="废品名称">
          <el-input
            v-model="queryParams.name"
            clearable
            style="width: 200px"
            placeholder="请输入废品名称"
            @keyup.enter="handleQuery"
          />
        </el-form-item>

        <el-form-item label="处理日期" style="width: 300px">
          <el-date-picker
            v-model="dateRange"
            value-format="YYYY-MM-DD"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          ></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          <!-- <el-button @click="advanced = !advanced">
                        <a style=" color: #448EF7;">
                            <el-icon v-if="advanced">
                                <ArrowUpBold />
                            </el-icon>
                            <el-icon v-else>
                                <ArrowDownBold />
                            </el-icon>
                        </a>
                    </el-button> -->
        </el-form-item>
      </el-row>
      <!-- <el-row v-if="advanced">
                <el-form-item label="处理日期" style="width: 308px;">
                    <el-date-picker v-model="dateRange" value-format="YYYY-MM-DD" type="daterange" range-separator="-"
                        start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
                </el-form-item>
            </el-row> -->
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Download" @click="handleExport">
          导出
        </el-button>
      </el-col>
    </el-row>

    <el-table
      v-loading="loading"
      border
      :data="handleList"
      style="margin-top: 20px"
      :summary-method="getSummaries"
      show-summary
    >
      <el-table-column
        label="序号"
        width="60"
        type="index"
        align="center"
      ></el-table-column>
      <el-table-column
        label="处理单编码"
        align="center"
        prop="wasteCode"
        width="200"
      >
        <template v-slot="{ row }">
          <el-button type="primary" @click="handleView(row)" text>
            {{ row.wasteCode }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column
        label="处理日期"
        width="120"
        align="center"
        prop="handleDate"
      />
      <el-table-column label="处理单位" align="center" prop="handleDept" />

      <el-table-column label="废品名称" align="center" prop="name" />
      <el-table-column label="规格型号" align="center" prop="specs" />

      <el-table-column label="单位" align="center" prop="unit">
        <template #default="scope">
          <dict-tag :options="mes_scrap_unit" :value="scope.row.unit" />
        </template>
      </el-table-column>

      <el-table-column label="数量" align="center" prop="quantity" />
      <el-table-column label="皮重/kg" align="center" prop="weightTare" />
      <el-table-column label="毛重/kg" align="center" prop="weightGross" />

      <el-table-column label="净重/kg" align="center" prop="weightNet" />
      <el-table-column label="单价" align="center" prop="price" />
      <el-table-column label="金额" align="center" prop="amount" />

      <el-table-column label="备注" align="center" prop="remark" />
    </el-table>
    <pagination
      style="margin-top: 0; padding-top: 0"
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script setup name="wasteDetail">
import { listDetail, wasteExport, exportDetail } from "@/api/scrap/handle.js";
const { proxy } = getCurrentInstance();
const { mes_scrap_unit } = proxy.useDict("mes_scrap_unit");
import { downLoadBlob } from "@/utils/ruoyi";
import { ElMessage, ElMessageBox } from "element-plus";
const router = useRouter();
const handleList = ref([]);
const loading = ref(false);
const total = ref(0);
const advanced = ref();
const currentcCount = ref({});
const showSearch = ref(true);
const getSummaries = (param) => {
  const { columns, data } = param;
  const sums = [];
  columns.forEach((column, index) => {
    if (index === 0) {
      sums[index] = "合计";
      return;
    }
    switch (column.property) {
      case "amount":
        sums[index] = currentcCount.value.sumAmount;
        break;
      case "weightTare":
        sums[index] = currentcCount.value.sumWeightTare;
        break;
      case "weightGross":
        sums[index] = currentcCount.value.sumWeightGross;
        break;
      case "weightNet":
        sums[index] = currentcCount.value.sumWeightNet;
        break;
      case "price":
        sums[index] = currentcCount.value.sumPrice;
        break;
      default:
        break;
    }
  });
  return sums;
};
const getDateNow = () => {
  var myDate = new Date();
  var year = myDate.getFullYear();
  var month = myDate.getMonth() + 1; //获取当前月份(0-11,0代表1月，所以要加1);
  var day = myDate.getDate(); //获取当前日（1-31）
  if (month < 10) {
    month = "0" + month;
  }
  if (day >= 0 && day <= 9) {
    day = "0" + day;
  }
  var firstDay = year + "-" + month + "-01";
  return firstDay;
};
const getCurrentDay = () => {
  var now = new Date();
  var year = now.getFullYear();
  var month = now.getMonth() + 1; // 月份从0开始，需要加1
  var day = now.getDate();
  if (month < 10) {
    month = "0" + month;
  }
  if (day >= 0 && day <= 9) {
    day = "0" + day;
  }
  const currentDay = year + "-" + month + "-" + day;
  return currentDay;
};
const dateRange = ref([getDateNow(), getCurrentDay()]);
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    wasteCode: null,
    handleDept: null,
    name: null,
    // status: 1,
  },
  rules: {
    code: [{ required: true, message: "处理单编码不能为空", trigger: "blur" }],
    handleDept: [
      { required: true, message: "处理单位不能为空", trigger: "blur" },
    ],
  },
});
const { queryParams, form, rules } = toRefs(data);
// 查看
const handleView = (row) => {
  router.push({
    path: "/scrap/handle/details",
    query: {
      id: row.id,
      readonly: true,
      code: row.wasteCode,
      status: row.status,
    },
  });
};

function getList() {
  loading.value = true;
  listDetail({
    ...queryParams.value,
    beginTime: Array.isArray(dateRange.value) ? dateRange.value[0] : undefined,
    endTime:
      Array.isArray(dateRange.value) > 0 ? dateRange.value[1] : undefined,
  }).then((response) => {
    currentcCount.value.sumPrice = response.rows[0]?.sumPrice;
    currentcCount.value.sumAmount = response.rows[0]?.sumAmount;
    currentcCount.value.sumWeightGross = response.rows[0]?.sumWeightGross;
    currentcCount.value.sumWeightTare = response.rows[0]?.sumWeightTare;
    currentcCount.value.sumWeightNet = response.rows[0]?.sumWeightNet;
    handleList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  queryParams.value.wasteCode = null;
  queryParams.value.handleDept = null;
  queryParams.value.name = null;
  dateRange.value = [getDateNow(), getCurrentDay()];
  proxy.resetForm("queryRef");
  handleQuery();
}
// 导出
const handleExport = () => {
  exportDetail(queryParams.value).then((res) => {
    downLoadBlob(res, "废料处理明细列表");
  });
};
getList();
</script>
